//
//  CycleViewController.m
//  HomeIn
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "CycleViewController.h"
#import "CycleModel.h"
#import "FirstPageModel.h"
#import "JingxuanCell.h"
#import "DetailViewController.h"

@interface CycleViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSMutableArray *modelArray;//model数组
@property (nonatomic, retain) UICollectionView *collectionView;

@end

@implementation CycleViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //视图将要出现的时候刷新
    [self.collectionView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.modelArray = [NSMutableArray array];
    
    //导航栏标题
    self.navigationItem.title = self.cycleModel.title;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //导航栏左侧按钮
    UIImage *image = [UIImage imageNamed:@"iconfont-unie642.png"];
    //取消渲染
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction)];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH - 20, HEIGHT / 4);
    //设置item的行间距
    flowLayout.minimumLineSpacing = 10;
    //设置item的上,左,下,右 边界距离
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    //注册cell
    [self.collectionView registerClass:[JingxuanCell class] forCellWithReuseIdentifier:@"reuse"];

    //请求cell数据
    [self getAFNetWorkingData:self.cycleModel.target_id];
    
}

- (void)getAFNetWorkingData:(NSInteger)ids
{
    NSString *urlString = [NSString stringWithFormat:@"http://api.bohejiaju.com/v1/collections/%ld/posts?gender=1&generation=2&limit=20&offset=0", self.cycleModel.target_id];
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        NSMutableDictionary *dictionary = responseObject;
        NSMutableArray *array = [[dictionary objectForKey:@"data"] objectForKey:@"posts"];
        
        for (NSMutableDictionary *dic in array)
        {
            FirstPageModel *firstPageModel = [[FirstPageModel alloc] init];
            [firstPageModel setValuesForKeysWithDictionary:dic];
            
            [self.modelArray addObject:firstPageModel];

        }

        [self.collectionView reloadData];
        
    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             NSLog(@"error = %@",error);
             //本地缓存
         }];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.modelArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //直接从cell重用池里取
    JingxuanCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.layer.masksToBounds = YES;//masksToBounds裁剪掉边框
    cell.layer.cornerRadius = 5;//设置圆角度
    
    FirstPageModel *firstPageModel = [self.modelArray objectAtIndex:indexPath.row];
    cell.firstPageModel = firstPageModel;
    
    return cell;
    
}

//点击item触发方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    FirstPageModel *firstPageModel = [self.modelArray objectAtIndex:indexPath.row];
    detailVC.firstPageModel = firstPageModel;//属性传值
    
    //push到详情的时候让tabBar(工具栏)消失
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (void)leftBarAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
